package com.knav.inject.LogInject;

import androidx.appcompat.app.AppCompatActivity;

import com.knav.inject.Base.Log;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

public class LogInjectHandle {

    /**
     * 注册日志注解
     *
     * @param clazz  使用到log的这个类  这个类必须要继承于 AppCompatActivity 才可以使用{@Log}这个注解
     */

    public static void registerLog(Class<? extends AppCompatActivity> clazz){

        Field[] declaredFields = clazz.getDeclaredFields();
        Constructor<?> resConstructor = null;
        for (int i = 0; i < declaredFields.length; i++) {
            declaredFields[i].setAccessible(true);
            Annotation[] annotations = declaredFields[i].getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation.annotationType().getName().equals("com.knav.inject.Base.Log")){

                    try {
                        if (declaredFields[i].getType().newInstance() instanceof ILog){
                            Class<?> fieldType = declaredFields[i].getType();
                            Constructor<?>[] constructors = fieldType.getConstructors();
                            for (Constructor<?> constructor : constructors) {
                                if (constructor.getParameters().length == 0 ){
                                    resConstructor = declaredFields[i].getType().getConstructor();
                                    if (resConstructor == null){
                                        throw new IllegalArgumentException("ILog 的实现类必须要构造方法");
                                    }
                                    declaredFields[i].set(clazz.newInstance() , resConstructor.newInstance() );
                                }else {
                                    for (Parameter parameter : constructor.getParameters()) {
                                        resConstructor = declaredFields[i].getType().getConstructor(parameter.getType());
                                        if (resConstructor == null){
                                            throw new IllegalArgumentException("ILog 的实现类必须要构造方法");
                                        }
                                        resConstructor.setAccessible(true);
                                        Log log = declaredFields[i].getAnnotation(Log.class);
                                        declaredFields[i].set(clazz.newInstance() , resConstructor.newInstance(log.tag()) );
                                    }
                                }
                            }
                        }else {
                            throw new IllegalArgumentException(declaredFields[i].getName()+"必须得是ILog接口得的实现类");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
//                Log.e("TAG", "registerLog: "+annotation.annotationType().getName() );

            }

        }
    }

}
